/**
 * Created by ChenChen on 2016/2/21.
 */

var vm=avalon.define({
    $id:"index",
    ready:function(){
    vm.toggle();
    vm.add();
    vm.deleteall();
    vm.getworkweeks();
    vm.datehide();
    vm.tocomplete(e);
    vm.toremove(e);
    vm.preload();
    vm.addtoTag1();
    vm.addtoTag2();
    vm.save();
    vm.quit();
    },
    /**读取localstorage**/
    Tag1:[],
    Tag2:[],
    todolist:[
    ],
    CompletedTodos:[
    ],
    preload:function() {
        var storage = window.localStorage;
        /**读取Completed并写入CompletedTodos**/
        if (vm.CompletedTodos.size() === 0) {
            for (i = 100; i < storage.length + 100; i++) {
                var getcomplete = storage.getItem(i);
                if (getcomplete !== null) {
                    var getcompletearray = JSON.parse(getcomplete);
                    vm.CompletedTodos.push(getcompletearray);
                } else {
                    continue
                }

            }
            /**读取todos并写入todoslist**/
            for (e = 10; e < storage.length + 10; e++) {
                var gettodos = storage.getItem(e);
                if (gettodos !== null) {
                    var gettodosarray = JSON.parse(gettodos);
                    vm.todolist.push(gettodosarray);
                } else {
                    continue
                }
            }
            /**读取tag并写入vm.tag中**/
            var getTag1 = storage.getItem(0);
            var getTag2 = storage.getItem(1);
            vm.Tag1=JSON.parse(getTag1);
            vm.Tag2=JSON.parse(getTag2);
        }
        else {
            return
        }
    },

    /**底部选项卡**/
    name:"ToDoList",
    Nav:[{
        title:"Histroy Todos",
        tagname:"Histroy",
        iconclass:"mui-icon-list",
        href:"histroy",
        vm:""
            },
        {
        title:"Today's Todos",
        tagname:"Today",
        iconclass:"mui-icon-flag",
        href:"todays",
        vm:""
        },
        {
        title:"Add Todos",
        tagname:"Add",
        iconclass:"mui-icon-compose",
        href:"addlist",
        vm:""
        },
        {
            title:"Settings",
            tagname:"Setting",
            iconclass:"mui-icon-gear",
            href:"setting",
            vm:""
        }
    ],
    en:'todays',
    toggle: function (i) {
             vm.en=vm.Nav[i].href;

    },
    /**增加todos模块**/
    select1:"",
    select2:"",
    txt:"",
    add:function(){
        if (vm.txt.length<1){
            alert('Please input the list item!')
        }else {
            var storage=window.localStorage;
            var temp={Tag1:vm.select1,Tag2:vm.select2,Description:vm.txt,CreateTime:new Date().getTime()};
            vm.todolist.push(temp);
            for (i=10;i<storage.length+10;i++){
                if (storage.getItem(i)!=null){
                    continue
                }else {
                    var settodos=JSON.stringify(temp);
                    storage.setItem(i,settodos);
                    break;
                }
            }
            alert('Add Todos Success!')
        }
    },
    deleteall:function(){
        vm.txt="";
        vm.select1="";
        vm.select2=""
    },

    /**显示当前日期模块**/
    currdate:"",
    currday:"",
    workweeks:"",
    datevisible:false,
    getworkweeks:function(){
        var d= new Date();
        var weekday=new Array(7);
        weekday[0]="Sunday";
        weekday[1]="Monday";
        weekday[2]="Tuesday";
        weekday[3]="Wednesday";
        weekday[4]="Thursday";
        weekday[5]="Friday";
        weekday[6]="Saturday";
        var i1=weekday[d.getDay()]
        vm.currdate= d;
        vm.currday= i1;
        // 定义变量d为当年的1月1日0点

        d.setFullYear(d.getFullYear(),0,1);
       // 定义day为当年1月1日的星期数(0为星期天,1为星期1,6为星期6)
        var day = d.getDay();
       // 定义fistweekleft为第一周剩余的天数,此处认为星期一是一周的第一天，如果将星期天定义为一周的第一天，请写成fistweekleft = (6-day)%6
        var fistweekleft = (7-day)%7;
       // 将d赋值为第二周的第一天,1+fistweekleft号为第一周最后天，1+fistweekleft+1为第二周第一天
        d.setFullYear(d.getFullYear(),0,1+fistweekleft+1);
       // 定义变量d1为当天
        var d1 = new Date();
       // 当前时间与当年第二周第一天的毫秒数之差除以一周的毫秒数并取整即为当前日期距本的第二周已过的周数
       // 结果加上2即为当天为本年的第几周（如果在一周的第一天的0点运行此程序，结果会比实际值大1，此种情况请自行处理
        var ww=2+parseInt((d1.getTime()-d.getTime())/1000/60/60/24/7);
        vm.workweeks=ww;
        vm.datevisible=true;
    },
    datehide:function(){
        vm.datevisible=false;
    },
    /** 增加到Tag**/
    inputTag:"",
    addtoTag1:function(){
        if (vm.inputTag.length<1){
            alert('Please input the Tag item!')
        }else {
           vm.Tag1.push(vm.inputTag);
           vm.inputTag=""
        }
    },
    addtoTag2:function(){
        if (vm.inputTag.length<1){
            alert('Please input the Tag item!')
        }else {
            vm.Tag2.push(vm.inputTag);
            vm.inputTag=""
        }
    },
    save:function(){
      var storage=window.localStorage;
        var temp1=JSON.stringify(vm.Tag1);
        var temp2=JSON.stringify(vm.Tag2);
        storage.removeItem(0);
        storage.removeItem(1);
        storage.setItem(0,temp1);
        storage.setItem(1,temp2);
        alert('Save Success!');
    },

    /**完成todos模块**/
    tocomplete:function(e){
        var btnArray = ['No', 'Yes'];
        mui.confirm('Complete this item', 'ToDos', btnArray, function(i) {
            if (i.index == 1) {
                var temparray={};
                temparray.Tag1=vm.todolist[e].Tag1,
                    temparray.Tag2=vm.todolist[e].Tag2,
                    temparray.Description=vm.todolist[e].Description;
                    var d=new Date();
                    temparray.CompleteTime= d.getTime();
                vm.CompletedTodos.push(temparray);
                var storage=window.localStorage;
                for (j=100;j<storage.length+100;j++){
                    if (storage.getItem(j)!=null){
                        continue
                    }else {
                        var setcomplete=JSON.stringify(temparray);
                        storage.setItem(j,setcomplete);
                        break
                    }
                }
                vm.todolist.removeAt(e);
            } else {
                return;
            }
        })

    },
    /**删除todos**/
    toremove:function(e){
        var btnArray = ['No', 'Yes'];
        window.confirm('Remove this item', 'ToDos', btnArray, function(i) {
            if (i.index == 1) {
                vm.todolist.removeAt(e);
            } else {
                return;
            }
        })

    },
    quit:function(){
        window.runtime.quit();
    }

})